﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace studentmanagementdemo
{
    public partial class frmclassthem : Form
    {

        private SqlConnection con;
        private DataTable dtClass = new DataTable("tblClass");
        private SqlDataAdapter da = new SqlDataAdapter();
        private Boolean kt;

        private void connect()
        {
            //String cn = "Data Source=127.0.0.1;Initial Catalog=iPMAC;Persist Security Info=True;User ID=sa;Password=sa";
            String cn = "Data Source=(local);Initial Catalog=iPMAC;Integrated Security=True";
            try
            {
                con = new SqlConnection(cn);
                con.Open(); //Moi ket noi                
            }
            catch (Exception ex)
            {
                MessageBox.Show("Không thể kết nối tới cơ sở dữ liệu !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void disconnect()
        {
            con.Close(); //Dong ket noi
            con.Dispose(); //Giai phong tai nguyen
            con = null; //Huy doi tuong
        }

        private void getdata()
        {
            SqlCommand command = new SqlCommand(); //Khai bao 1 command
            command.Connection = con; //Ket noi
            command.CommandType = CommandType.Text; //Khai bao kieu command
            command.CommandText = "Select * from tblClass"; //Cau truy van SQL
            da.SelectCommand = command; //Gan command cho da            
            da.Fill(dtClass); //Nap du lieu vao table              
        }

        public frmclassthem()
        {
            InitializeComponent();
        }

        private bool kiemtra()
        {
            kt = true;
            int i;
            if (txttenlop.Text == "")
            {
                MessageBox.Show("Bạn chưa nhập tên lớp học !", "THÔNG BÁO", MessageBoxButtons.OK);
                kt = false;
            }
            else if (Int32.TryParse((txtsiso.Text), out i) == false)
            {
                MessageBox.Show("Sĩ số phải là kiểu số !", "THÔNG BÁO", MessageBoxButtons.OK);
                kt = false;
            }
            return kt;
        }

        private void btnluu_Click(object sender, EventArgs e)
        {
            kiemtra();
            if (kt == true)
            {
                DataRow row = dtClass.NewRow();
                row["fldClassName"] = txttenlop.Text;
                row["fldTime"] = txtcahoc.Text;
                row["fldCapacity"] = txtsiso.Text;
                dtClass.Rows.Add(row);
                SqlCommand commandInsert = new SqlCommand();
                commandInsert.Connection = con;
                commandInsert.CommandType = CommandType.Text;
                commandInsert.CommandText = @"Insert tblClass (fldClassName, fldTime, fldCapacity) 
                                                       Values(@fldClassName, @fldTime, @fldCapacity)";
                commandInsert.Parameters.Add("@fldClassName", SqlDbType.NVarChar, 50, "fldClassName");
                commandInsert.Parameters.Add("@fldTime", SqlDbType.NVarChar, 50, "fldTime");
                commandInsert.Parameters.Add("@fldCapacity", SqlDbType.Int, 50, "fldCapacity");
                da.InsertCommand = commandInsert;
                da.Update(dtClass);
                MessageBox.Show("Bạn đã thêm thành công !", "THÔNG BÁO", MessageBoxButtons.OK);
                Close();
                disconnect();
                Dispose();
                frmclass _frmclass = new frmclass();
                _frmclass.Show();
            }
        }

        private void frmclassthem_Load(object sender, EventArgs e)
        {
            connect();
            getdata();
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            Close();
            Dispose();
            disconnect();
            frmclass _frmclass = new frmclass();
            _frmclass.Show();
        }

        private void btnhuy_Click(object sender, EventArgs e)
        {
            txttenlop.Text = "";
            txtcahoc.Text = "";
            txtsiso.Text = "";
        }
    }
}
